from flask import Flask, render_template, request
import query
from flask import Blueprint
from MySQL import mysql_c
from flask_cors import CORS

student_api = Blueprint('student_api', __name__)
CORS(student_api)	# 注册CORS
# 学生登录接口
@student_api.route('/student_login', methods=['get', 'post'])
def logins():
    if (request.method == 'POST'):
        username = request.json.get('username')
        password = request.json.get('password')
    else:
        username = request.values.get('username')
        password = request.values.get('password')

        if username and password:
            backinf = query.student_login_check(mysql_c,username,password)
            if backinf == "密码正确":
                return {"msg": str(backinf), "state": 1}
            else:
                return {"msg": str(backinf), "state": -1}
        else:
            return {"msg": "数据不完整", "state": -1}

# 学生注册接口
@student_api.route('/student_re', methods=['get', 'post'])
def regs():
    if (request.method == 'POST'):
        opusername = request.json.get('opusername')
        name = request.json.get('name')
        username = request.json.get('username')
        password = request.json.get('password')
    else:
        opusername = request.values.get('opusername')
        name = request.values.get('name')
        username = request.values.get('username')
        password = request.values.get('password')

        if opusername and name and username and password:
            backinf = query.student_register(mysql_c,opusername,username,name,password)
            if backinf == "成员插入成功":
                return {"msg":str(backinf),"state":1}
            else:
                return {"msg":str(backinf),"state":-1}
        else:
            return {"msg":"数据不完整","state":-1}

# 学生查看自己全部班级接口
@student_api.route('/student_all_classes', methods=['get', 'post'])
def all_classess():
    if (request.method == 'POST'):
        username = request.json.get('username')
    else:
        username = request.values.get('username')
        if username:
            backinf = query.get_student_classes(mysql_c,username)
            if backinf[0] == -1:
                return {"msg": "获取失败", "state": -1, "data": None}
            elif backinf[0] == 1:
                return {"msg": "获取成功", "state": 1, "data": backinf[1]}
        else:
            return {"msg": "数据不完整", "state": -1, "data": None}

#学生查看自己某项作业的成绩
@student_api.route('/student_check_job_score', methods=['get', 'post'])
def checksingals():
    if (request.method == 'POST'):
        username = request.json.get('username')
        jobid = request.json.get('jobid')
    else:
        username = request.values.get('username')
        jobid = request.values.get('jobid')

    if username:
        backinf = query.get_job_score(mysql_c, username,jobid)
        if backinf[0] == -1:
            return {"msg": "获取失败", "state": -1, "data": None}
        elif backinf[0] == 1:
            return {"msg": "获取成功", "state": 1, "data": backinf[1]}
    else:
        return {"msg": "数据不完整", "state": -1, "data": None}

#学生查看自己某一门课的成绩
@student_api.route('/student_check_class_score', methods=['get', 'post'])
def checkclassscore():
    if (request.method == 'POST'):
        username = request.json.get('username')
        classid = request.json.get('classid')
    else:
        username = request.values.get('username')
        classid = request.values.get('classid')

    if username:
        backinf = query.get_class_score(mysql_c, username,classid)
        if backinf[0] == -1:
            return {"msg": "获取失败", "state": -1, "data": None}
        elif backinf[0] == 1:
            return {"msg": "获取成功", "state": 1, "data": backinf[1]}
    else:
        return {"msg": "数据不完整", "state": -1, "data": None}

#学生查看自己已经完成的作业有哪几轮
@student_api.route('/student_work_number', methods=['get', 'post'])
def checkwork():
    if (request.method == 'POST'):
        username = request.json.get('username')#学生用户名
        classid = request.json.get('classid')#课程id
    else:
        username = request.values.get('username')
        classid = request.values.get('classid')

    if username:
        backinf = query.get_student_homework(mysql_c, username, classid)
        if backinf[0] == -1:
            return {"msg": "获取失败", "state": -1, "data": None}
        elif backinf[0] == 1:
            return {"msg": "获取成功", "state": 1, "data": backinf[1]}
    else:
        return {"msg": "数据不完整", "state": -1, "data": None}